package com.yunxin.common.utils.oss;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;

/**
 * OSS图片参数解析器
 * @author zero°
 * @since 2023-06-05
 */
public enum OssPhotoOptionsParser {

    /**
     * 单例
     */
    X;

    public String parse(List<OssPhotoOption> options) {
        StringJoiner content = new StringJoiner("/", "image/", "");
        if (Objects.nonNull(options)) {
            options.forEach(option -> {
                StringJoiner part = new StringJoiner(",");
                if (OssPhotoOperationTypeEnum.RESIZE == option.operationType()) {
                    OssPhotoResizeOption resizeOption = (OssPhotoResizeOption) option;
                    part.add(resizeOption.operationType().getOp());
                    Optional.ofNullable(resizeOption.getMode()).ifPresent(v -> part.add(v.getMode()));
                    Optional.ofNullable(resizeOption.getHeight()).ifPresent(v -> part.add("h_" + v));
                    Optional.ofNullable(resizeOption.getHeight()).ifPresent(v -> part.add("w_" + v));
                    Optional.ofNullable(resizeOption.getLargestSide()).ifPresent(v -> part.add("l_" + v));
                    Optional.ofNullable(resizeOption.getShortestSide()).ifPresent(v -> part.add("s_" + v));
                    Optional.ofNullable(resizeOption.getLimit()).ifPresent(v -> part.add("limit_" + v));
                    Optional.ofNullable(resizeOption.getColor()).ifPresent(v -> part.add("color_" + v));
                    Optional.ofNullable(resizeOption.getPercent()).ifPresent(v -> part.add("p_" + v));
                } else if (OssPhotoOperationTypeEnum.QUALITY_EXCHANGE == option.operationType()) {
                    OssPhotoQualityOption qualityOption = (OssPhotoQualityOption) option;
                    part.add(qualityOption.operationType().getOp());
                    Optional.ofNullable(qualityOption.getQuality()).ifPresent(v -> part.add("q_" + v));
                    Optional.ofNullable(qualityOption.getAbsoluteQuality()).ifPresent(v -> part.add("Q_" + v));
                }
                if (part.length() > 0) {
                    content.add(part.toString());
                }
            });
        }
        return content.toString();
    }

}
